package demo1.Decorator;

/**
 * @ClassName Decorator
 * @Description TODO
 * @Author 耀酱
 * @Date 2023/6/5 13:15
 * @Version 1.0
 */

public class DecoratorTest {
    public static void main(String[] args) {
        FastFood fastFood = new FriedNoodles();
        System.out.println(fastFood.getDesc()+"||"+fastFood.getPrice());

        FastFood fastFood1 = new FriedRice();
        System.out.println(fastFood1.getDesc()+"||"+fastFood1.getPrice());

        Egg egg = new Egg(fastFood);
        egg.cost();
        egg.getDesc();





    }
}

abstract class FastFood{
    private float price;
    private String desc;

    public FastFood(float price,String desc){
        this.price = price;
        this.desc = desc;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
    // 计算价格的方法
    public abstract float cost();
}
class FriedRice extends FastFood{

    public FriedRice() {
        super(10,"炒米");
    }

    @Override
    public float cost() {
        return getPrice();
    }
}
class FriedNoodles extends FastFood{
    public FriedNoodles() {
        super(15,"炒面");
    }

    @Override
    public float cost() {
        return getPrice();
    }
}
// 抽象装饰者角色
abstract class Garnish extends FastFood{
    private FastFood fastFood;

    public Garnish(FastFood fastFood,float price,String desc){
        super(price,desc);
        this.fastFood = fastFood;

    }

    public FastFood getFastFood() {
        return fastFood;
    }

    public void setFastFood(FastFood fastFood) {
        this.fastFood = fastFood;
    }
}
// 配料类
class Egg extends Garnish{

    public Egg(FastFood fastFood){
        super(fastFood,1.5f,"加鸡蛋一个1.5元");
    }
    // 计算价格
    @Override
    public float cost() {
        return getFastFood().cost() + getPrice();
    }

    @Override
    public String getDesc() {
        return super.getDesc() + getFastFood().getDesc();
    }
}